package com.zhuizikejithreeig.cn.threeigkeji.loginfo;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.zhuizikejithreeig.cn.been.HaveSetBeen;
import com.zhuizikejithreeig.cn.been.NewLoginBeen;
import com.zhuizikejithreeig.cn.contect.ContectUtils;
import com.zhuizikejithreeig.cn.contect.UriContect;
import com.zhuizikejithreeig.cn.threeigkeji.BasicActivity;
import com.zhuizikejithreeig.cn.threeigkeji.CompontApplayCation;
import com.zhuizikejithreeig.cn.threeigkeji.R;
import com.zhuizikejithreeig.cn.utils.GsonUtil;
import com.zhuizikejithreeig.cn.utils.ToastMake;
import com.zhuizikejithreeig.cn.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;
import okhttp3.Call;

public class ChioceSetActivity extends BasicActivity implements View.OnClickListener {
    private static final int READSDK = 203;
    private ImageView mImage1;
    private ImageView mImage2;
    private ImageView mImage3;
    private Button    mName8;
    private ImageView mImagemian;
    private String    mUserId;
    private List<String> positions = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chioce_set);
        showBody();
        mImage1 = (ImageView) findViewById(R.id.image1);
        mImage2 = (ImageView) findViewById(R.id.image2);
        mImage3 = (ImageView) findViewById(R.id.image3);
        mImagemian = (ImageView) findViewById(R.id.imagemian);
        mName8 = (Button) findViewById(R.id.name8);

        mImage1.setOnClickListener(this);
        mImagemian.setOnClickListener(this);
        mImage2.setOnClickListener(this);
        mImage3.setOnClickListener(this);
        mName8.setOnClickListener(this);


        mUserId = Utils.getString(ContectUtils.IDSAVE);


    }

    @Override
    protected void onResume() {
        super.onResume();
        if (TextUtils.isEmpty(mUserId)) {
            skipOtherActivity(LoginActivity.class, true);
        } else {
            //TODO:获取已经绑定的座椅
            if (isConnected()) {
                //                        logIn(pho, paw);

                havePostion();
            } else {
                ToastMake.showToast(getResources().getString(R.string.string57));
            }

        }
    }

    private void havePostion() {
        showProgrees("座椅加载中......");
        OkHttpUtils
                .post()
                .url(UriContect.GETUSERBINDS)
                .addParams("id", mUserId)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastMake.showToast(getResources().getString(R.string.string58));
                        hideProgree();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        hideProgree();
                        HaveSetBeen been = GsonUtil.parseJsonToBean(response, HaveSetBeen.class);
                        if (been == null) {
                            ToastMake.showToast(getResources().getString(R.string.string58));
                            return;
                        }
                        if (!TextUtils.equals(been.getStatus(), "200")) {
                            ToastMake.showToast(been.getMsg());
                            return;
                        } else {

                            positions.clear();
                            List<HaveSetBeen.DataBean> data = been.getData();
                            if (data.size() != 0) {

                                for (int i = 0; i < data.size(); i++) {
                                    positions.add(data.get(i).getPosition());
                                    setUnSelict(data.get(i).getPosition());
                                }
                            }

                        }
                    }
                });


    }

    private void setUnSelict(String s) {
        if (TextUtils.equals(s,"2")){
            mImage1.setEnabled(false);
            mImage1.setImageDrawable(getResources().getDrawable(R.mipmap.select_qian));
        }else  if (TextUtils.equals(s,"3")){
            mImage2.setEnabled(false);
            mImage2.setImageDrawable(getResources().getDrawable(R.mipmap.hou_left));
        }else  if(TextUtils.equals(s,"4")){
            mImage3.setEnabled(false);
            mImage3.setImageDrawable(getResources().getDrawable(R.mipmap.hou_right));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image1:
                CompontApplayCation.postion = "2";
                mImage1.setImageDrawable(getResources().getDrawable(R.mipmap.select_qian));
                mImage2.setImageDrawable(getResources().getDrawable(R.mipmap.touminghou));
                mImage3.setImageDrawable(getResources().getDrawable(R.mipmap.touminghou));
                initSelect();
                break;
            case R.id.image2:
                CompontApplayCation.postion = "3";
                mImage2.setImageDrawable(getResources().getDrawable(R.mipmap.hou_left));
                mImage1.setImageDrawable(getResources().getDrawable(R.mipmap.seat));
                mImage3.setImageDrawable(getResources().getDrawable(R.mipmap.touminghou));
                initSelect();
                break;
            case R.id.image3:
                CompontApplayCation.postion = "4";
                mImage3.setImageDrawable(getResources().getDrawable(R.mipmap.hou_right));
                mImage1.setImageDrawable(getResources().getDrawable(R.mipmap.seat));
                mImage2.setImageDrawable(getResources().getDrawable(R.mipmap.touminghou));
                initSelect();
                break;
            //开始扫码
            case R.id.name8:
                if (ContextCompat.checkSelfPermission(ChioceSetActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    ToastMake.showToast(getResources().getString(R.string.string69));
                    hasReadPermission();

                } else {
                    String id = Utils.getString(ContectUtils.IDSAVE);
                    if (TextUtils.isEmpty(id)){

                        skipOtherActivity(LoginActivity.class,true);
                        return;
                    }
                    startActivity(new Intent(ChioceSetActivity.this, BundingSetActivity.class));
                    finish();
                }


                break;

            case R.id.imagemian:
                ToastMake.showToast("主驾无需扫码");
                break;
        }
    }

    private void initSelect() {

        for (int i = 0; i < positions.size(); i++) {
            setUnSelict(positions.get(i));
        }
    }

    private void hasReadPermission() {
        PermissionGen.with(ChioceSetActivity.this)
                .addRequestCode(READSDK)
                .permissions(
                        Manifest.permission.CAMERA
                )
                .request();

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @PermissionSuccess(requestCode = READSDK)
    public void requestPhotoSuccess() {
        //成功之后的处理
        //.......
        startActivity(new Intent(ChioceSetActivity.this, BundingSetActivity.class));
        finish();
    }

    @PermissionFail(requestCode = READSDK)
    public void requestPhotoFail() {
        //失败之后的处理，我一般是跳到设置界面

        boolean showRequestPermission = ActivityCompat.shouldShowRequestPermissionRationale(ChioceSetActivity.this, Manifest.permission.CAMERA);
        if (showRequestPermission) {
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.setData(Uri.parse("package:" + getPackageName()));
            startActivity(intent);
        }


    }
}
